﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OrionApplication.OrionLib.Orion;

namespace OrionApplication.OrionForm
{
    public partial class OrionGenClasseForm : OrionBaseSimpleForm
    {
        public OrionGenClasseForm(String TitreP)
            : base(TitreP)
        {
            InitializeComponent();

        }

        public OrionGenClasseForm() 
            : base()
        {
            InitializeComponent();
        }

        public void RempliList()
        {
            List<string> lTable = OrionOutils.GetListTable();
            foreach (string table in lTable)
                kClbChoixTable.Items.Add(table);
        }

        public void GenClasseAndDao()
        {
            this.kryptonRichTextBox1.Text = string.Empty;

            for (Int32 i = 0; i < kClbChoixTable.Items.Count; i++)
            {
                if (kClbChoixTable.GetItemChecked(i))
                {
                    this.GenClasse(kClbChoixTable.Items[i].ToString());
                    this.GenDao(kClbChoixTable.Items[i].ToString());
                }
            }
        }

        private void GenClasse(string nomTable)
        {
            List<OrionColonne> lCol = OrionOutils.GetListColumn(nomTable);
            string s = "using System;" + Environment.NewLine +
                        "" + Environment.NewLine +
                        "namespace OrionApplication.OrionLib" + Environment.NewLine +
                        "{" + Environment.NewLine +
                        "public class " + nomTable + " : ClasseBase" + Environment.NewLine +
                        "{" + Environment.NewLine;
            foreach (OrionColonne oc in lCol)
            {
                s += "public " + oc.Type + " " + oc.Nom + " { get; set; }" + Environment.NewLine;
            }
            s += "" + Environment.NewLine +
                    "public " + nomTable + "()" + Environment.NewLine +
                        ": base()" + Environment.NewLine +
                        "{" + Environment.NewLine;
            foreach (OrionColonne oc in lCol)
            {
                if (oc.Type.Equals("Int32"))
                    s += "this." + oc.Nom + " = 0;" + Environment.NewLine;
                if (oc.Type.Equals("String"))
                    s += "this." + oc.Nom + " = String.Empty;" + Environment.NewLine;
                if (oc.Type.Equals("Double"))
                    s += "this." + oc.Nom + " = 0.0;" + Environment.NewLine;
                if(oc.Type.Equals("Boolean"))
                    s += "this." + oc.Nom + " = true;" + Environment.NewLine;
            }
            s += "}" + Environment.NewLine +
                "}" + Environment.NewLine +
                "}" + Environment.NewLine + Environment.NewLine + Environment.NewLine;

            this.kryptonRichTextBox1.Text += s;
        }

        private void GenDao(string nomTable)
        {
            List<OrionColonne> lCol = OrionOutils.GetListColumn(nomTable);
            string s = "using System;" + Environment.NewLine +
                        "using System.Collections.Generic;" + Environment.NewLine +
                        "using System.Runtime.Remoting.Messaging;" + Environment.NewLine +
                        "using MySql.Data.MySqlClient;" + Environment.NewLine +
                        "using OrionApplication.OrionLib.Orion;" + Environment.NewLine +
                        "" + Environment.NewLine +
                        "namespace OrionApplication.OrionLib.Dao" + Environment.NewLine +
                        "{" + Environment.NewLine +
                        "public class Dao" + nomTable + " : DaoBase" + Environment.NewLine +
                        "{" + Environment.NewLine +
                        "" + Environment.NewLine +
                        "public Dao" + nomTable + "()" + Environment.NewLine +
                        ": base()" + Environment.NewLine +
                        "{" + Environment.NewLine +
                        "this.TableName = \"" + nomTable + "\";" + Environment.NewLine +
                        "this.ViewName = \"" + nomTable + "\";" + Environment.NewLine +
                        "}" + Environment.NewLine +
                        "" + Environment.NewLine +
                        "public " + nomTable + " SelectById(Int32 OrionId)" + Environment.NewLine +
                        "{" + Environment.NewLine +
                        "lParam.Clear();" + Environment.NewLine +
                        nomTable + " retour = new " + nomTable + "();" + Environment.NewLine +
                        "String req = this.DebutSelect" + Environment.NewLine +
                        "+ this.TableName" + Environment.NewLine +
                        "+ \" WHERE OrionId=@orionid\";" + Environment.NewLine +
                        "" + Environment.NewLine +
                        "lParam.Add(new MySqlParameter(\"@OrionId\", OrionId));" + Environment.NewLine +
                        "" + Environment.NewLine +
                        "MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);" + Environment.NewLine +
                        "" + Environment.NewLine +
                        "if (dr.Read())" + Environment.NewLine +
                        "retour = ChargeFromReader(dr);" + Environment.NewLine +
                        "dr.Close();" + Environment.NewLine +
                        "return retour;" + Environment.NewLine +
                        "}" + Environment.NewLine +
                        "" + Environment.NewLine +
                        "public List<" + nomTable + "> SelectAllActif()" + Environment.NewLine +
                        "{" + Environment.NewLine +
                        "lParam.Clear();" + Environment.NewLine +
                        "List<" + nomTable + "> retour = new List<" + nomTable + ">();" + Environment.NewLine +
                        "String req = this.DebutSelect" + Environment.NewLine +
                        "+ this.TableName" + Environment.NewLine +
                        "+ \" WHERE OrionEtat=1\";" + Environment.NewLine +
                        "" + Environment.NewLine +
                        "MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);" + Environment.NewLine +
                        "" + Environment.NewLine +
                        "while (dr.Read())" + Environment.NewLine +
                        "retour.Add(ChargeFromReader(dr));" + Environment.NewLine +
                        "dr.Close();" + Environment.NewLine +
                        "" + Environment.NewLine +
                        "return retour;" + Environment.NewLine +
                        "}" + Environment.NewLine +
                        "" + Environment.NewLine +
                        "public List<" + nomTable + "> SelectAll()" + Environment.NewLine +
                        "{" + Environment.NewLine +
                        "lParam.Clear();" + Environment.NewLine +
                        "List<" + nomTable + "> retour = new List<" + nomTable + ">();" + Environment.NewLine +
                        "String req = this.DebutSelect" + Environment.NewLine +
                        "+ this.TableName;" + Environment.NewLine +
                        "" + Environment.NewLine +
                        "MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);" + Environment.NewLine +
                        "" + Environment.NewLine +
                        "while (dr.Read())" + Environment.NewLine +
                        "retour.Add(ChargeFromReader(dr));" + Environment.NewLine +
                        "dr.Close();" + Environment.NewLine +
                        "" + Environment.NewLine +
                        "return retour;" + Environment.NewLine +
                        "}" + Environment.NewLine +
                        "" + Environment.NewLine +
                        "private " + nomTable + " ChargeFromReader(MySqlDataReader dr)" + Environment.NewLine +
                        "{" + Environment.NewLine +
                        "int i = 0;" + Environment.NewLine +
                        nomTable + " retour = new " + nomTable + "();" + Environment.NewLine +
                        "retour.OrionId = dr.GetInt32(\"OrionId\");" + Environment.NewLine +
                        "" + Environment.NewLine;
                        

            foreach (OrionColonne oc in lCol)
            {
                s += "i++;" + Environment.NewLine + 
                    "if (dr.IsDBNull(i))" + Environment.NewLine;

                if (oc.Type.Equals("Int32"))
                {
                    s += "retour." + oc.Nom + " = 0;" + Environment.NewLine;
                    s += "else" + Environment.NewLine;
                    s += "retour." + oc.Nom + " = dr.GetInt32(\"" + oc.Nom + "\");" + Environment.NewLine;
                }
                else if (oc.Type.Equals("String"))
                {
                    s += "retour." + oc.Nom + " = String.Empty;" + Environment.NewLine;
                    s += "else" + Environment.NewLine;
                    s += "retour." + oc.Nom + " = dr.GetString(\"" + oc.Nom + "\");" + Environment.NewLine;
                }
                else if (oc.Type.Equals("Double"))
                {
                    s += "retour." + oc.Nom + " = 0.0;" + Environment.NewLine;
                    s += "else" + Environment.NewLine;
                    s += "retour." + oc.Nom + " = dr.GetDouble(\"" + oc.Nom + "\");" + Environment.NewLine;
                }
                else if (oc.Type.Equals("DateTime"))
                {
                    s += "retour." + oc.Nom + " = null;" + Environment.NewLine;
                    s += "else" + Environment.NewLine;
                    s += "retour." + oc.Nom + " = dr.GetDateTime(\"" + oc.Nom + "\");" + Environment.NewLine;
                }
                else if (oc.Type.Equals("Boolean"))
                {
                    s += "retour." + oc.Nom + " = null;" + Environment.NewLine;
                    s += "else" + Environment.NewLine;
                    s += "retour." + oc.Nom + " = dr.GetBoolean(\"" + oc.Nom + "\");" + Environment.NewLine;
                }
            }

            s += "" + Environment.NewLine +
                    "retour.OrionCrea = dr.GetDateTime(\"OrionCrea\");" + Environment.NewLine +
                    "retour.OrionMaj = dr.GetDateTime(\"OrionMaj\");" + Environment.NewLine +
                    "retour.OrionUserId = dr.GetInt32(\"OrionUserId\");" + Environment.NewLine +
                    "retour.OrionEtat = dr.GetBoolean(\"OrionEtat\");" + Environment.NewLine +
                     "" + Environment.NewLine +
                    "return retour;" + Environment.NewLine +
                    "}" + Environment.NewLine +
                    "" + Environment.NewLine +
                    "public " + nomTable + " Update(" + nomTable + " oe)" + Environment.NewLine +
                    "{" + Environment.NewLine +
                    "lParam.Clear();" + Environment.NewLine +
                    "oe.OrionMaj = DateTime.Now;" + Environment.NewLine +
                    "" + Environment.NewLine +
                    nomTable + " retour = new " + nomTable + "();" + Environment.NewLine +
                    "String req = \"UPDATE \" + TableName + \" SET ";
            
            int i=0;
            foreach(OrionColonne oc in lCol)
            {
                if(i!=0)
                    s += ",";
                s += oc.Nom + "=@" + oc.Nom;
                i++;
            }

            s += ",OrionUserId=@OrionUserId, OrionMaj=@OrionMaj";
                    
            s += " WHERE OrionId=@OrionId\";" + Environment.NewLine +
                "" + Environment.NewLine;

            foreach(OrionColonne oc in lCol)
                s += "lParam.Add(new MySqlParameter(\"@" + oc.Nom + "\", oe." + oc.Nom + "));" + Environment.NewLine;

            s += "" + Environment.NewLine +
                    "lParam.Add(new MySqlParameter(\"@OrionUserId\", ((OrionUser)CallContext.GetData(OrionConfBase.OrionUserConnected)).OrionId));" + Environment.NewLine +
                    "lParam.Add(new MySqlParameter(\"@OrionMaj\", oe.OrionMaj));" + Environment.NewLine +
                    "lParam.Add(new MySqlParameter(\"@OrionId\", oe.OrionId));" + Environment.NewLine +
                    "" + Environment.NewLine +
                    "this.ExecuteNonQuery(req, lParam);" + Environment.NewLine +
                    "retour = this.SelectById(oe.OrionId);" + Environment.NewLine +
                    "" + Environment.NewLine +
                    "return retour;" + Environment.NewLine +
                    "}" + Environment.NewLine +
                    "" + Environment.NewLine +
                    "public " + nomTable + " Insert(" + nomTable + " oe)" + Environment.NewLine +
                    "{" + Environment.NewLine +
                    "lParam.Clear();" + Environment.NewLine +
                    "oe.OrionCrea = DateTime.Now;" + Environment.NewLine +
                    "oe.OrionMaj = DateTime.Now;" + Environment.NewLine +
                    "" + Environment.NewLine +
                    "" + nomTable + " retour = new " + nomTable + "();" + Environment.NewLine +
                    "String req = \"INSERT INTO \" + TableName + \"(OrionId";

            foreach (OrionColonne oc in lCol)
                s += "," + oc.Nom;
            
            s += ",OrionEtat, OrionUserId, OrionCrea, OrionMaj)  VALUES(NULL";
            
            foreach (OrionColonne oc in lCol)
                s += ",@" + oc.Nom;

            s += ",@OrionEtat,@OrionUserId,@OrionCrea,@OrionMaj)\";";
            s += "" + Environment.NewLine;

            foreach (OrionColonne oc in lCol)
                s += "lParam.Add(new MySqlParameter(\"@" + oc.Nom + "\", oe." + oc.Nom + "));" + Environment.NewLine;

            s += "lParam.Add(new MySqlParameter(\"@OrionEtat\", oe.OrionEtat));" + Environment.NewLine +
                    "lParam.Add(new MySqlParameter(\"@OrionUserId\", ((OrionUser)CallContext.GetData(OrionConfBase.OrionUserConnected)).OrionId));" + Environment.NewLine +
                    "lParam.Add(new MySqlParameter(\"@OrionCrea\", oe.OrionCrea));" + Environment.NewLine +
                    "lParam.Add(new MySqlParameter(\"@OrionMaj\", oe.OrionMaj));" + Environment.NewLine +
                    "" + Environment.NewLine +
                    "this.ExecuteNonQuery(req, lParam);" + Environment.NewLine +
                    "retour = this.SelectById(Convert.ToInt32(this.LastInsertId));" + Environment.NewLine +
                    "" + Environment.NewLine +
                    "return retour;" + Environment.NewLine +
                    "}" + Environment.NewLine +
                    "}" + Environment.NewLine +
                    "}" + Environment.NewLine;

            this.kryptonRichTextBox1.Text += s;

        }
    }
}
